import { _decorator } from 'cc';
import { DIRECTION_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM } from '../../Enums';
import EventManager from '../../Runtime/EventManager';
import { EntityManager } from '../../Base/EntityManager';
import DataManager from '../../Runtime/DataManager';
import { WoodenSkeletonStateMachine } from './WoodenSkeletonStateMachine';
import { EnemyManager } from '../../Base/EnemyManager';
import { IEntity } from '../../Levels';
const { ccclass, property } = _decorator;



@ccclass('WoodenSkeletonManager')
export class WoodenSkeletonManager extends EnemyManager {
    async init(params:IEntity) {
        this.fsm = this.addComponent(WoodenSkeletonStateMachine)
        await this.fsm.init()
        super.init(params)
 
        //监听敌人攻击
        EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE_END, this.onAttack, this)

    }



    /**敌人攻击玩家 */
    onAttack() {
        if (!DataManager.Instance.player || this.state === ENTITY_STATE_ENUM.DEATH) {
            return;
        }
        //拿到player的坐标
        const { x: playerX, y: playerY, state: playerState } = DataManager.Instance.player
        if (playerState === ENTITY_STATE_ENUM.DEATH || playerState === ENTITY_STATE_ENUM.AIR_DEATH) {
            return;
        }
        if ((this.x === playerX && Math.abs(this.y - playerY) <= 1) || (this.y === playerY && Math.abs(this.x - playerX) <= 1)) {
            this.state = ENTITY_STATE_ENUM.ATTACK
            //发送通知玩家死亡
            EventManager.Instance.emit(EVENT_ENUM.ATTACK_PLAYER, ENTITY_STATE_ENUM.DEATH, this)
        } else {
            this.state = ENTITY_STATE_ENUM.IDLE
        }

    }

    onDestroy() {
        super.onDestroy()

        EventManager.Instance.off(EVENT_ENUM.PLAYER_MOVE_END, this.onAttack)

    }


}


